package com.findMoneyGame.pages.game {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;

	import flash.events.MouseEvent;
	import flash.events.Event;
	
	import flash.geom.Matrix;
	import flash.geom.Rectangle;

	import com.findMoneyGame.models.User;
	import com.findMoneyGame.models.GameLevel;
	import com.findMoneyGame.models.Model;
	import com.findMoneyGame.tools.ImageAdjuster;
	import com.findMoneyGame.pages.game.HintContour;
	import com.findMoneyGame.pages.shop.ImprovementsConfig;

	public class GameImage extends MovieClip {
		public static const MONEY_FOUNDED : String = "moneyFounded";
		public static const TRYING_FAILED : String = "tryingFailed";

		private var gameLevel : GameLevel;
		private var user : User;
		private var bitmap : Bitmap;
		private var scaledBitmap : Bitmap;
		private var originalImage : BitmapData;
		private var loupeContour : LoupeContour = new LoupeContour();
		private var hintContour : HintContour = new HintContour();

		public function get loupeEnabled () : Boolean { return contains(scaledBitmap); }
		public function get hintEnabled () : Boolean { return contains(hintContour); }


		public function GameImage(user : User, gameLevel : GameLevel, bitmap : Bitmap) {
			this.user = user;
			this.user.addEventListener(Model.UPDATE, userUpdate);
			this.gameLevel = gameLevel;
			var r = gameLevel.targetRectangle;
			hintContour.x = r.x + r.width / 2;
			hintContour.y = r.y + r.height / 2;

			this.originalImage = gameLevel.image;
		   	this.bitmap = bitmap;
			
		   	refresh();
		   	addEventListener(MouseEvent.CLICK, clickEventHandler);
		   	addChild(bitmap);
		}
		public function enableLoupe () : void {
			addChild(scaledBitmap);
			addChild(loupeContour);
			addEventListener(Event.ENTER_FRAME, enterFrame);
		}
		public function disableLoupe () : void {
			removeChild(scaledBitmap);
			removeChild(loupeContour);
			removeEventListener(Event.ENTER_FRAME, enterFrame);
		}
		public function enableHint () : void {
			addChild(hintContour);
			//
		}
		public function disableHint () : void {
			removeChild(hintContour);
			//
		}

		private function userUpdate (e : Event) {
			refresh();
			//
		}
		private function clickEventHandler (e : MouseEvent) : void {
			var r = gameLevel.targetRectangle;
			if (r.contains(e.localX, e.localY)) {
				dispatchEvent(new Event(GameImage.MONEY_FOUNDED));
			} else {
				dispatchEvent(new Event(GameImage.TRYING_FAILED));
			}
		}
		private function enterFrame (e : Event) {
			var scale = 1 - ImprovementsConfig.getLoupeScale(user.loupeBonusIndex);
			scaledBitmap.x = mouseX * scale;
			scaledBitmap.y = mouseY * scale;
			loupeContour.x = mouseX;
			loupeContour.y = mouseY;
		}
		private function refresh () {
			var hintRadius = ImprovementsConfig.getHintRadius(user.hintBonusIndex);
			hintContour.width = hintRadius * 2;
			hintContour.height = hintRadius * 2;
			
			//init scaled bitmap
			var loupeScale = ImprovementsConfig.getLoupeScale(user.loupeBonusIndex);
			var w = bitmap.width * loupeScale;
			var h = bitmap.height * loupeScale;
			var scaledImage = ImageAdjuster.adjust(originalImage, w, h);
			var newScaledBmp = new Bitmap(scaledImage);
			if (scaledBitmap && contains (scaledBitmap)) {
				removeChild(scaledBitmap);
				scaledBitmap = newScaledBmp;
				scaledBitmap.mask = loupeContour;
				addChild(scaledBitmap);
			} else {
				scaledBitmap = newScaledBmp;
				scaledBitmap.mask = loupeContour;
			}
		}
	}

}